#ifndef __GLOBAL_H__
#define __GLOBAL_H__


#include "gd32f10x.h" 



//==============================================================================
// Conditional Compile
//==============================================================================


//==============================================================================
#define  FAULT_CODE_NO  0 // system fault code
#define  FAULT_CODE_M1_NO_CURRENT    1
#define  FAULT_CODE_M2_NO_CURRENT    2
#define  FAULT_CODE_M1_NO_HALL       3
#define  FAULT_CODE_M2_NO_HALL       4  
#define  FAULT_CODE_M1_OVER_CURRENT  5
#define  FAULT_CODE_M2_OVER_CURRENT  6 
#define  FAULT_CODE_IMBALENCE        7 
#define  FAULT_CODE_PARAM_ERROR      8 


//==============================================================================
#define  HS_STA_BUF_SIZE  4
extern uint16_t HSStaBuf[HS_STA_BUF_SIZE];      


//==============================================================================
#define  BOX_STA_BUF_SIZE  4
extern uint16_t BoxStaBuf[BOX_STA_BUF_SIZE];


//==============================================================================
// Custom Data Type
//==============================================================================
#define  SYSFLAG_SYSMODE_NORMAL  0
#define  SYSFLAG_SYSMODE_RST     1

#define  ACCE_MODE  0
#define  DECE_MODE  1

#define  RUN_DIR_UP    0
#define  RUN_DIR_DOWN  1

typedef union
{
    struct
    { 
        uint32_t PhaseLoss   :1; //  
        uint32_t HallFail    :1; //
        uint32_t Rsvd02      :1; //  
        uint32_t Rsvd03      :1; //
        uint32_t Rsvd04      :1; //
        uint32_t Rsvd05      :1;      
        uint32_t Rsvd06      :1;  
        uint32_t Rsvd07      :1;    

        uint32_t Rsvd08      :1; // 1-command order to stop        
        uint32_t Rsvd09      :1; //      
        uint32_t CurrBase    :1; //           
        uint32_t ParamError  :1; // 1-        
        uint32_t Rsvd12      :1;
        uint32_t Rsvd13      :1; 
        uint32_t CommandStop :1;   
        uint32_t Calibration :1;        

        uint32_t RunEnable   :1; // 
        uint32_t PwmEnable   :1; // 
        uint32_t StopDone    :1; //        
        uint32_t AcceDece    :1; //  
        uint32_t DirCCW      :1; //       
        uint32_t Energized   :1; //        
        uint32_t Rsvd22      :1; //   
        uint32_t StudyStart  :1; 

        uint32_t Rsvd24     :1; 
        uint32_t Rsvd25     :1;
        uint32_t Rsvd26     :1;         
        uint32_t Rsvd27     :1;  
        uint32_t Rsvd28     :1; 
        uint32_t DataBackup    :2;// 0-idle 1-erase 2-erasing 3-program
        uint32_t DataBackupIdle:1;
    }bit;
    uint32_t Word;
}tSYSFLAG;
#define tSYSFLAG_DEFAULT {\
    0,0,0,0,0,0,0,0,\
    0,0,0,0,0,0,1,1,\
    0,0,0,0,0,0,0,0,\
    0,0,0,0,0,0,0}
#define tSYSFLAG_STOP_BITS 0x0000FFFF
#define tSYSFLAG_PROT_BITS 0x00000FFF   
 
     
//==============================================================================
// Global Variable
//==============================================================================
extern tSYSFLAG SysFlag;

extern uint8_t TickTockCount;
extern uint8_t TickTock;
extern uint8_t TickTockSlowCnt;
extern uint8_t TickTockSlowPrd;
extern uint8_t TickTockFastCnt;
extern uint8_t TickTockFastPrd;

extern uint32_t OpTime;
extern uint32_t Version;

extern uint32_t RstInfo;


//==============================================================================
// Global Function
//==============================================================================
extern void VarInit(void);
extern void VarUpdate(void);
extern void time_delay_Nms(uint8_t N);

#endif


//==============================================================================
// End of file.
//==============================================================================
